package com.jiazhong.cloud.example.student.student.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiazhong.cloud.example.student.commons.bean.Student;
import com.jiazhong.cloud.example.student.commons.mapper.StudentMapper;
import com.jiazhong.cloud.example.student.commons.util.JsonResult;
import com.jiazhong.cloud.example.student.commons.util.ResultTool;
import com.jiazhong.cloud.example.student.student.feign.CourseFeign;
import com.jiazhong.cloud.example.student.student.feign.SchoolFeign;
import com.jiazhong.cloud.example.student.student.service.StudentService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private CourseFeign courseFeign;
    @Autowired
    private SchoolFeign schoolFeign;

    @Override
    public JsonResult saveStudent(Student student) {
        save(student);
        return ResultTool.success("success");
    }

    @Cacheable(cacheNames = "findStudentById")
    public JsonResult findStudentById(String id) {
        // 只能得到学生信息
        Student student = studentMapper.selectById(id);
        // 根据学生的专业ID获取到专业信息
        JsonResult course = courseFeign.findCourseById(student.getCourseId());
        // 根据学生的学校ID获取到学校信息
        JsonResult school = schoolFeign.findSchoolById(student.getSchoolId());
        // 组装
        student.setCourse(course);
        student.setSchool(school);
        return ResultTool.success(student);
    }

    @Cacheable(cacheNames = "findStudentByNo", key = "#stuNo")
    public JsonResult findStudentByNo(String stuNo) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stu_no", stuNo);
        // 只能得到学生信息
        Student student = studentMapper.selectOne(queryWrapper);
        // 根据学生的专业ID获取到专业信息
        JsonResult course = courseFeign.findCourseById(student.getCourseId());
        // 根据学生的学校ID获取到学校信息
        JsonResult school = schoolFeign.findSchoolById(student.getSchoolId());
        // 组装
        student.setCourse(course);
        student.setSchool(school);
        return ResultTool.success(student);
    }
}
